#include "Ui.h"
#include <iostream>
Ui::DispatchResults Ui :: dispatchUiEvent(int event)
{
	switch(event)
	{
		case ' ' ... '~':	//Printable ASCII
			lb.addToLine(event);
			return Success;
		case KEY_IC:		//Insert Key - Change Input mode
			inpt_mode = (Ui::InputModes)((inpt_mode+1)%Modes);
			return Success;
		case KEY_ENTER:	//Keypad with no numlock
		case 10:		//Normal Enter key
			if(autocomp_sel != -1)
			{
				lb.addToLine(autocomp->at(0).second[autocomp_sel].substr(lb.getPos()));
				autocomp_sel = -1;
			}
			else
			{
				return ExecCmd;
			}
			return Success;
		case KEY_BACKSPACE:
			lb.erasePrevChar();
			return Success;
		case KEY_LEFT:
			if(lb.getPos())
				lb.setPos(lb.getPos()-1);
			return Success;
		case KEY_RIGHT:
			if(lb.getPos() != lb.getCurrentLineSize())
				lb.setPos(lb.getPos()+1);
			return Success;
		case KEY_UP:
			if(autocomp)
			{
				if(autocomp_sel < ((int)autocomp->at(0).second.size())-1)
					autocomp_sel++;
				std::cerr << "autocomp_sel:" << autocomp_sel << " , " << autocomp->at(0).second.size() << "\n";
			}
			return Success;
		case KEY_DOWN:
			if(autocomp)
			{
				if(autocomp_sel >= 0)
					autocomp_sel--;
				std::cerr << "autocomp_sel:" << autocomp_sel << " , " << autocomp->at(0).second.size() << "\n";
			}
			return Success;
		case KEY_HOME:
			lb.setPos(0);
			return Success;
		case KEY_END:
			lb.setPos(lb.getCurrentLineSize());
			return Success;
		case KEY_DC:
			lb.eraseNextChar();
			return Success;
		default:
			return Invalid;
	}
}